﻿using System.Reflection;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Castle.MicroKernel.SubSystems.Configuration;

namespace Inovout
{
    public abstract class ComponentRegistrar : IWindsorInstaller,IFrameworkComponentRegistrar,IBusinessComponentRegistrar
    {
        //ComponentRegistration
        private IWindsorContainer container;
  


        public IWindsorContainer Container
        {
            get { return container; }
         }
        private IConfigurationStore store;
        public IConfigurationStore Store
        {
            get { return store; }
        }
        private IApplication application;
        public IApplication Application
        {
            get 
            {
                if (application == null)
                {
                    application=container.Resolve<IApplication>();
                }
                return application;
            }
        }
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            this.container = container;
            this.store = store;

            container.Register(
                Component.For<ComponentRegistrar>()
                .Instance(this)
                .OnCreate((kenal, instance) =>
                    {
                        instance.store = store;
                        instance.container = container;
                    })
                  .LifeStyle.Singleton
                );
            Register();
        }

        public abstract void Register();

        public virtual void Register(Assembly assembly) { }
    }
}